﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace VisualRibbonEditor
{
	public enum WellKnownTab
	{
		Unknown = 0,
		Main = 1,
		Related = 2,
		View = 3,
		Chart = 4,
		Visualization = 5,
		Customize = 99
	}
	public partial class Tab : UserControl
	{
		private const string SelectedTabBorderColor = "#FF8f959a";

		public Tab()
		{
			InitializeComponent();
		}

		private string _displayName;
		public string DisplayName
		{
			get { return _displayName; }
			set { _displayName = value; NameTextBlock.Text = value; }
		}

		public string CrmId { get; set; }

		// Returns well-known tab type
		public WellKnownTab WellKnownTab { get { return Tab.GetWellKnownTab(CrmId); } }

		// Detects well-known tab type by id, or WellKnownTab.Unknown
		public static WellKnownTab GetWellKnownTab(string id)
		{
			WellKnownTab tabType = WellKnownTab.Unknown; // Default
			if (!String.IsNullOrEmpty(id))
			{
				if (id.StartsWith("Mscrm."))
				{
					string end = id.Substring(id.LastIndexOf('.'));
					switch (end)
					{
						case ".Developer":
							tabType = WellKnownTab.Customize;
							break;
						case ".View":
							tabType = WellKnownTab.View;
							break;
						case ".Chart":
							tabType = WellKnownTab.Chart;
							break;
						case ".VisualizationTab":
							tabType = WellKnownTab.Visualization;
							break;
						case ".MainTab":
							tabType = WellKnownTab.Main;
							break;
						case ".Related":
							tabType = WellKnownTab.Related;
							break;
					}
				}
			}
			return tabType;
		}

		private bool _isHilighted = false;
		public bool IsHilighted
		{
			get { return _isHilighted; }
			set
			{
				_isHilighted = value;
				 
				if (_isHilighted)
				{
					BottomBorder.BorderBrush = new SolidColorBrush(Colors.White);
					TabBorder.BorderBrush = new SolidColorBrush(GetColorFromHexa(SelectedTabBorderColor));
				}
				else
				{

					BottomBorder.BorderBrush = new SolidColorBrush(GetColorFromHexa("#ffb6babf"));
					TabBorder.BorderBrush = new SolidColorBrush(Colors.White);
				}

				TabBorder.Background = new SolidColorBrush(Colors.White);

			}
		}

		public static Color GetColorFromHexa(string hexaColor)
		{
			return
				Color.FromArgb(
					Convert.ToByte(hexaColor.Substring(1, 2), 16),
					Convert.ToByte(hexaColor.Substring(3, 2), 16),
					Convert.ToByte(hexaColor.Substring(5, 2), 16),
					Convert.ToByte(hexaColor.Substring(7, 2), 16)
				);
		}

		private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			if (Click != null)
			{
				Click(this, new RoutedEventArgs());
			}

			//hilight this tab
			this.IsHilighted = true;

			//un-hilight any other tabs in the same panel.
			DependencyObject parent = VisualTreeHelper.GetParent(this);
			if (parent is Panel)
			{
				Panel parentPanel = (Panel)parent;
				foreach (var item in parentPanel.Children)
				{
					if (item is Tab && item != this)
					{
						Tab otherTab = (Tab)item;
						if (otherTab.IsHilighted)
						{
							otherTab.IsHilighted = false;
						}
					}
				}
			}
		}

		public event RoutedEventHandler Click;

		private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			if (IsEnabled)
			{
				this.NameTextBlock.Foreground = new SolidColorBrush(Colors.Black);
			}
			else
			{			 
				this.NameTextBlock.Foreground = new SolidColorBrush(Colors.LightGray);
			}
		}

		private void UserControl_MouseEnter(object sender, MouseEventArgs e)
		{
			if (!IsHilighted)
			{
				LinearGradientBrush br = new LinearGradientBrush();
				br.GradientStops.Add(new GradientStop() { Color = GetColorFromHexa("#FFF1F1F1"), Offset = 0 });
				br.GradientStops.Add(new GradientStop() { Color = Colors.White, Offset = 1 });
				br.EndPoint = new Point(0.5, 1);
				br.StartPoint = new Point(0.5, 0);
				TabBorder.Background = br;

				TabBorder.BorderBrush = new SolidColorBrush(GetColorFromHexa("#FFD3D6D9"));				 
			}
		}

		private void UserControl_MouseLeave(object sender, MouseEventArgs e)
		{
			TabBorder.Background = new SolidColorBrush(Colors.White);

			if (_isHilighted)
			{
				BottomBorder.BorderBrush = new SolidColorBrush(Colors.White);
				TabBorder.BorderBrush = new SolidColorBrush(GetColorFromHexa(SelectedTabBorderColor));
			}
			else
			{

				BottomBorder.BorderBrush = new SolidColorBrush(GetColorFromHexa("#ffb6babf"));
				TabBorder.BorderBrush = new SolidColorBrush(Colors.White);
			}
		}

	}
}
